package kulstats;

import java.util.ArrayList;
import kulstats.*;

public class CalculatePoints {

	private User currentUser;
	private UserManager userManager;
	private ActivityManager activityManager;
	private CourseManager courseManager;
	
	public CalculatePoints()
	{
		this.userManager = Registry.userManager();
		this.activityManager = Registry.activityManager();
		this.courseManager = Registry.courseManager();
		this.currentUser = userManager.getCurrentUser();
	}
		
	public Integer calculatePoints(String courseName, String type, int tracktime)
	{
		float points = 0;
		float studyPoints = 0;
		float difficultyFactor = 0;
		float activityFactor = 0;
		System.out.print(currentUser);
		ArrayList<Course> allCourses = courseManager.getAllCourses();
		
		// Database
		for(Course course : allCourses) {
			if(course.getName().equals(courseName)){
					studyPoints = course.getStudypoints();	 
					difficultyFactor = ((100-(float)course.getSuccessrate())/100);
					}
				}
		if(type.equals("lesson"))
		{
			activityFactor = 2;
		}
			
		if(type.equals("study"))
		{
			activityFactor = 3/* 55 voor DEMO met natuurkunde voor 100 punten*/;
		}
		for(Integer i=0;i<=tracktime && i<300; i++)	
		points = points + (studyPoints*difficultyFactor*activityFactor*((300-(float)i)/2700));
		Integer result = (int) points;
	
		return result;
	}
	
	public void updatePoints() {
	    Activity lastActivity = activityManager.getLastActivity(currentUser);
		long stopTime = lastActivity.getStopDate().getTime();
		long startTime = lastActivity.getStartDate().getTime();
	    ActivityType trackType = lastActivity.getAType();
		long trackTime = stopTime-startTime;
		Integer trackedTime = (int) trackTime/60000;
		
		if(trackType == ActivityType.STUDY)
		{
			Study st = (Study) lastActivity;
			String courseName = st.getCourse().getName();
			Integer toAddPoints = calculatePoints(courseName,"study",trackedTime);
			Integer oldPoints = currentUser.getPoints();
			Integer newPoints = toAddPoints+oldPoints;
			
			System.out.println("Tracktime: "+trackedTime); 					
			System.out.println("coursename:" + courseName + ", toadd: "+toAddPoints+", old: "+oldPoints+", newpoints:"+newPoints );
			
			if(st != null) {
				st.setPoints(toAddPoints);
				activityManager.updateActivity(st);
	    	    currentUser.updatePoints(toAddPoints);
	    	    userManager.updateUser(currentUser.getName(), currentUser.getUserID(),currentUser.getAllCourseIDs(),UserType.STUDENT, newPoints);
			}
		}
		if(trackType == ActivityType.LESSON)
		{
			Lesson le = (Lesson) lastActivity;
			String courseName2 = le.getCourse().getName();
			Integer toAddPoints2 = calculatePoints(courseName2,"lesson",trackedTime);
			Integer oldPoints2 = currentUser.getPoints();
			Integer newPoints2 = toAddPoints2+oldPoints2;
			
			System.out.println("coursename:" + courseName2 + ", toadd: "+toAddPoints2+", old: "+oldPoints2+", newpoints:"+newPoints2 );
			
			if(le != null) 
				{
				le.setPoints(toAddPoints2);
				activityManager.updateActivity(le);
	    	    currentUser.updatePoints(toAddPoints2);
	    	    userManager.updateUser(currentUser.getName(), currentUser.getUserID(),currentUser.getAllCourseIDs(),UserType.STUDENT, newPoints2);
				}
		}
		if(trackType == ActivityType.SPARE_TIME)
		{}
	}
	
	public int calculateGraphBounds(String s)
	{
		int result = 0;
		int top = 0;
	if(s.equals("top"))
		{
		int points = currentUser.getPoints();
		int i = 100;
			while(points>=i)
			{
				i = i*10;	 
			}
		result=i;
		}
	if(s.equals("bottom"))
	{
		int points = currentUser.getPoints();
		int i = 100;
			while(points>=i)
			{
				i = i*10;	 
			}
		top = i;
		if(top==100)
		{
			result=0;
		}
		else
		{
		result = i/10;	
		}
		}
		return result;	
    }
	
	public int calculateGraphPercentage()
	{
		int percentage = (currentUser.getPoints()-calculateGraphBounds("bottom"))*100/(calculateGraphBounds("top")-
							calculateGraphBounds("bottom"));
		int top = calculateGraphBounds("top");
		int bot = calculateGraphBounds("bottom");		
		
		System.out.println(percentage+", "+top+", "+bot+ ", "+ currentUser.getPoints() );
	 return percentage;
	}	
	
	public int calculateMilestonePoints(Milestone milestone)
	{
		int result = 0;
		Course course = milestone.getCourse();
		Integer minutes = milestone.getHours()*60;
		String name = course.getName();
		
		int points = calculatePoints(name,"study",minutes);
		
		result = points*(minutes/(60*100));
		
		return result;		
	}
}